package 力扣_链表.基础操作;

/**
 * @author zx
 * @create 2022-08-15 15:32
 */
public class 删除排序链表中的重复元素_83 {
    public ListNode deleteDuplicates(ListNode head) {
        ListNode dummy = new ListNode(101);
        dummy.next = head;
        ListNode prev = dummy;
        while(head != null){
            ListNode next = head.next;
            if(next == null){
                break;
            }
            if(head.val == next.val){
                while(next != null && head.val == next.val){
                    next = next.next;
                    head = head.next;
                }
                //prev -> 第一个重复节点的前驱
                //next -> 最后一个重复节点的后继
                //prev -> head是需要删除的元素,保留head，既保留了一个重复元素
                prev.next = head;
            }else{
                head = head.next;
                prev = prev.next;
            }
        }
        return dummy.next;
    }
}
class ListNode {
    int val;
    ListNode next;
    ListNode(int val){
        this.val = val;
        next = null;
    }
}
